﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Threading;

#endregion

namespace ScrumTable.Common.Monitoring
{
    /// <summary>
    /// This class is used to observe an import status. It can be implemented
    /// for different platforms and frameworks.
    /// </summary>
    /// <remarks>
    /// The current class synchronizes the calls with the given data synchronizer.
    /// </remarks>
    public class DataImportMonitorSync : IDataImportMonitor
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IDataSyncDispatcher _syncDispatcher;
        private readonly IDataImportMonitor _importMonitorToSync;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataImportMonitorSync instance with the specified data.
        /// </summary>
        /// <param name="syncDispatcher">Specifies the dispatcher which is used ot synchronize the calls.</param>
        /// <param name="importMonitorToSync">Specifies the import monitor to synchronize.</param>
        public DataImportMonitorSync(IDataSyncDispatcher syncDispatcher, IDataImportMonitor importMonitorToSync)
        {
            PreCondition.AssertNotNull(syncDispatcher, "syncDispatcher");
            PreCondition.AssertNotNull(importMonitorToSync, "importMonitorToSync");

            _syncDispatcher = syncDispatcher;
            _importMonitorToSync = importMonitorToSync;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Calling this method determins that a new sub import progress has
        /// been started. This means that an additional progress bar should
        /// be displayed.
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the ReportProgress / EndSubProgress methods.</param>
        public void StartSubProgress(string identifier)
        {
            _syncDispatcher.BeginInvoke(() => _importMonitorToSync.StartSubProgress(identifier));
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        ///   references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state in percent. The maximum is 100.</param>
        public void ReportProgress(string identifier, byte current)
        {
            _syncDispatcher.BeginInvoke(() => _importMonitorToSync.ReportProgress(identifier, current));
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        ///   references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state.</param>
        /// <param name="max">Specifies the maximum.</param>
        public void ReportProgress(string identifier, int current, int max)
        {
            _syncDispatcher.BeginInvoke(() => _importMonitorToSync.ReportProgress(identifier, current, max));
        }

        /// <summary>
        /// Reports the end of a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the StartSubProgress / ReportProgress methods.</param>
        /// <param name="e">This argument will contain an exception if the progress has been aborted.</param>
        public void EndSubProgress(string identifier, Exception e)
        {
            _syncDispatcher.BeginInvoke(() => _importMonitorToSync.EndSubProgress(identifier, e));
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
